package com.jbh.recordlibrary.v;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;

/**
 * 实时绘制音频图形
 */
public class RecordWaveSurfaceView extends SurfaceView implements SurfaceHolder.Callback {


    /**
     *
     */
    private SurfaceHolder holder;

    /**
     * 绘制线的颜色
     *
     * 默认颜色
     */
    private int lineColor = Color.GRAY;
    /**
     * 绘制音频图形颜色
     *
     */
    private int recordLineColor = Color.GRAY;
    private int recordLineWidth = 4;

    /**
     * 绘制线条的宽度
     * px
     */
    private int lineWidth = 2;
    /**
     * 距离上下边界线的距离
     */
    private int line_off ;
    /**
     * 绘制图形的基线
     *
     */
    private int baseLine ;
    /**
     *
     */
    private int drawHeight;
    /**
     *
     */
    private Canvas canvas;

    private Paint mPaint ;

    public RecordWaveSurfaceView setLineWidth(int lineWidth) {
        this.lineWidth = lineWidth;
        return this;
    }

    public RecordWaveSurfaceView setRecordLineColor(int recordLineColor) {
        this.recordLineColor = recordLineColor;
        return this;
    }

    public RecordWaveSurfaceView setLineColor(int lineColor) {
        this.lineColor = lineColor;
        return this;
    }

    public RecordWaveSurfaceView setLine_off(int line_off) {
        this.line_off = line_off;
        return this ;
    }

    public RecordWaveSurfaceView(Context context) {
        this(context,null);
    }

    public RecordWaveSurfaceView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RecordWaveSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        holder = getHolder() ;
        holder.addCallback(this);
        initDrawRecordPaint();
    }


    private void initDrawRecordPaint() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setColor(recordLineColor);
        mPaint.setStrokeWidth(recordLineWidth);
    }


    @Override
    public void surfaceCreated( SurfaceHolder surfaceHolder) {
        initSurfaceView();
    }

    @Override
    public void surfaceChanged( SurfaceHolder surfaceHolder, int i, int i1, int i2) {
    }

    @Override
    public void surfaceDestroyed( SurfaceHolder surfaceHolder) {
    }


    /**
     *
     * @author tcx
     * init surfaceview
     *
     * 初始化绘制线条
     *
     */
    public  void initSurfaceView(){
        new Thread(){
            public void run() {
                /**
                 * 画布的高度
                 * 绘制声波图的范围
                 */
                drawHeight = getHeight()-line_off;
                baseLine = getHeight() / 2 ;
                /**
                 * 关键:获取画布
                 */
                canvas = getHolder().lockCanvas(new Rect(0, 0, getWidth(),getHeight()));
                /**
                 * 清除背景
                 */
//				canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
                canvas.drawColor(Color.WHITE);
                drawTopLine();
                drawCenterLine();
                drawBottomLine();
                /* 解锁画布，提交画好的图像*/
                getHolder().unlockCanvasAndPost(canvas);
            }
        }.start();
    }





    /**
     * 中心线
     */
    private void drawCenterLine() {
        Paint centerLine =new Paint();
        centerLine.setColor(lineColor);
        centerLine.setAntiAlias(true);
        centerLine.setDither(true);
        centerLine.setStrokeWidth(lineWidth);
        canvas.drawLine(0, getHeight()/2, getWidth() ,getHeight()/2, centerLine);
    }





    /**
     * 最下面的那根线
     */
    private void drawBottomLine() {
        Paint paintBottomLine =new Paint();
        paintBottomLine.setColor(lineColor);
        paintBottomLine.setStrokeWidth(lineWidth);
        paintBottomLine.setAntiAlias(true);
        paintBottomLine.setDither(true);
        canvas.drawLine(0, getHeight() - line_off/2, getWidth(), getHeight()-line_off/2, paintBottomLine);
    }




    /**
     * 最上面的那根线
     */
    private void drawTopLine() {
        Paint paintTopLine =new Paint();
        paintTopLine.setColor(lineColor);
        paintTopLine.setStrokeWidth(lineWidth);
        paintTopLine.setAntiAlias(true);
        paintTopLine.setDither(true);
        canvas.drawLine(0, line_off/2, getWidth(), line_off/2, paintTopLine);
    }



    /**
     * 这里是绘制音频图形
     */
    public void drawRecord(float divider,ArrayList<Short> buf) throws JSONException {
        Log.v("drawRecord","drawRecord = "+new JSONObject().put("",buf).toString());
        canvas = getHolder().lockCanvas(new Rect(0, 0, getWidth(),getHeight()));
        canvas.drawColor(Color.WHITE);
        canvas.save();
        if (canvas!=null){
            drawTopLine();
            drawCenterLine();
            drawBottomLine();
            for (int i = 0; i < buf.size(); i++) {
                byte bus[] = getBytes(buf.get(i));
                buf.set(i, (short)((0x0000 | bus[1]) << 8 | bus[0]));//高低位交换
            }

            for (int i = 0; i < buf.size(); i++) {

                double val = buf.get(i);
                if (val<5){
                    val = 5 ;
                }

                float radio = (float) (val *1.0 / 100);

                float y = radio * baseLine;
                /**当前指定位置绘制*/
                float currentDX = (i * divider)-2  ;
                /*中间出波形*/
                canvas.drawLine(currentDX, baseLine-y, currentDX,baseLine + y, mPaint);
            }
            canvas.restore();
            holder.unlockCanvasAndPost(canvas);// 解锁画布，提交画好的图像
        }
    }







    /**
     * 高低位交换
     *
     * @param s
     * @return
     */
    public byte[] getBytes(short s) {
        byte[] buf = new byte[2];
        for (int i = 0; i < buf.length; i++) {
            buf[i] = (byte) (s & 0x00ff);
            s >>= 8;
        }
        return buf;
    }



}
